package edu.neu.swc.cloud.controller;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.cloud.domain.Bide;
import edu.neu.swc.cloud.domain.Equipment;
import edu.neu.swc.cloud.domain.Order;
import edu.neu.swc.cloud.domain.Plan;
import edu.neu.swc.cloud.domain.QueryInfo;
import edu.neu.swc.cloud.service.BideService;
import edu.neu.swc.cloud.service.EquipmentService;
import edu.neu.swc.cloud.service.OrderService;
import edu.neu.swc.cloud.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/myOrder")
public class MyOrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    PlanService planService;

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    BideService bideService;


    @RequestMapping("/myOrderList")
    public String myOrderList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = orderService.getMyOrderCounts("%" + queryInfo.getQuery() + "%",queryInfo.getFac());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Order> myOrderList = orderService.findMyOrderByQuery("%" + queryInfo.getQuery() + "%",queryInfo.getFac(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",myOrderList);
        System.out.println("总条数："+numbers);
        System.out.println(myOrderList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/addBide")
    public String addBide(@RequestBody Bide bide){
        bideService.addBide(bide);
        return "success";
    }

    @RequestMapping("/isPlaned")
    public String isPlaned(int id){
        String flag = "error";
        Order order = orderService.findOrdById(id);
        if(order.getOrd_state() == 3 || order.getOrd_state() == 2){
            flag = "success";
        }
        return flag;
    }

    @RequestMapping("/planList")
    public String planList(QueryInfo queryInfo){
        int numbers = planService.getPlanCounts(queryInfo.getOrd());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Plan> planList = planService.findPlanByQuery(queryInfo.getOrd(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",planList);
        System.out.println("总条数："+numbers);
        System.out.println(planList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/addPlan")
    public String addPlan(@RequestBody Plan plan){
        planService.addPlan(plan);
        return "success";
    }
}
